/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jprojetocloseup;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.spi.DirStateFactory.Result;
import javax.swing.JOptionPane;

/**
 *
 * @author Administrador
 */
public class Update {

    public static Connection con;
    

    public void setUpdate(String sql) throws IOException {
        con = Conexao.getConnetion();
        try {
            PreparedStatement pst = con.prepareStatement(sql);
            if (JOptionPane.showConfirmDialog(null, " Confirma a gravação? ", " Pergunta do Sistema ", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
                pst.executeUpdate();
                JOptionPane.showMessageDialog(null, "Sucesso!");
            } else {
                con.close();
            }

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex);
            try {
                con.close();
            } catch (SQLException ex1) {
                JOptionPane.showMessageDialog(null, ex1);
                Logger.getLogger(Update.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
    }

    public void setDelete(String sql) throws IOException {
        con = Conexao.getConnetion();
        try {
            PreparedStatement pst = con.prepareStatement(sql);
            if (JOptionPane.showConfirmDialog(null, " Confirma a exclusão? ", " Pergunta do Sistema ", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
                pst.executeUpdate();
                JOptionPane.showMessageDialog(null, "Sucesso!");
            } else {
                con.close();
            }

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex);
            try {
                con.close();
            } catch (SQLException ex1) {
                JOptionPane.showMessageDialog(null, ex1);
                Logger.getLogger(Update.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
    }

    public void setUpdateDireto(String sql) throws IOException {
        con = Conexao.getConnetion();
        try {
            PreparedStatement pst = con.prepareStatement(sql);

            pst.executeUpdate();

            con.close();


        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex);
            try {
                con.close();
            } catch (SQLException ex1) {
                JOptionPane.showMessageDialog(null, ex1);
                Logger.getLogger(Update.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
    }

    public String getId(String campo) throws IOException {

        con = Conexao.getConnetion();

        try {
            ResultSet rsId = null;
            Statement stId = null;

            stId = con.createStatement();

            rsId = stId.executeQuery("select valor from controle where nome = '" + campo + "'");

            while (rsId.next()) {
                return rsId.getString(1);
            }

            con.close();


        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex + " setconferencia1");
            try {
                con.close();
            } catch (SQLException ex1) {
                JOptionPane.showMessageDialog(null, ex1 + "setconferencia2");
                Logger.getLogger(Update.class.getName()).log(Level.SEVERE, null, ex1 + " setconferenci3");
            }
        }
        return null;
    }

  

    public Boolean getExiste(String coluna, String tabela, String criterio) throws IOException {

        con = Conexao.getConnetion();

                try {
                    ResultSet rsId = null;
                    Statement stId = null;

                    stId = con.createStatement();

                    rsId = stId.executeQuery("select " + coluna + " from " + tabela + " where " + criterio);

                    while (rsId.next()) {
                        return  true;
                    }


                    con.close();



                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(null, ex + " setconferencia4");
            try {
                con.close();
            } catch (SQLException ex1) {
                JOptionPane.showMessageDialog(null, ex1 + "setconferencia5");
                Logger.getLogger(Update.class.getName()).log(Level.SEVERE, null, ex1 + " setconferencia5");
            }
                }
        return false;


            }
    

    public String getValor(String coluna, String tabela, String criterio) throws IOException {

        con = Conexao.getConnetion();

        try {
            ResultSet rsId = null;
            Statement stId = null;

            stId = con.createStatement();

            rsId = stId.executeQuery("select " + coluna + " from " + tabela + " where " + criterio);

            while (rsId.next()) {
                return rsId.getString(1);
            }


            con.close();



        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex + " setconferencia6");
            try {
                con.close();
            } catch (SQLException ex1) {
                JOptionPane.showMessageDialog(null, ex1 + "setconferencia7");
                Logger.getLogger(Update.class.getName()).log(Level.SEVERE, null, ex1 + " setconferencia8");
            }
        }
        return null;
    }

    public void setUpdateControle(String sql) throws IOException {
        con = Conexao.getConnetion();
        try {
            PreparedStatement pst = con.prepareStatement(sql);

            pst.executeUpdate();

            con.close();


        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex);
            try {
                con.close();
            } catch (SQLException ex1) {
                JOptionPane.showMessageDialog(null, ex1);
                Logger.getLogger(Update.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
    }
}
